﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BigLotterySyndicate.Classes;
using SyndicateDAO.DatabaseObject;
using System.Web.UI.HtmlControls;
using SyndicateBusiness.Classes;

namespace BigLotterySyndicate.Secured.Payments
{
    public partial class CreateMulti : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            if (!IsUserLoggedIn)
                return;

            this.PageTitle += " | Add multiple funds";
            this.MasterPage.SetTopMenuTab(global::BigLotterySyndicate.Secured.Controls.TopNav.AdminNavSection.Payments);

            SetList();
        }

        private void SetList()
        {
            if (Page.IsPostBack)
                return;

            BindList();
        }

        private void BindList()
        {
            var query = this.BusinessFacade.UserBL.GetAllBySyndicateId(this.LoggedInUser.syndicateId);

            if (query.Count() > 0)
            {
                this.RepeaterList.DataSource = query;
                this.RepeaterList.ItemDataBound += new RepeaterItemEventHandler(RepeaterList_ItemDataBound);
                this.RepeaterList.DataBind();
              
            }
        }

        void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            user p = e.Item.DataItem as user;

            ((System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Item.FindControl("chkAction")).Value = String.Format("userId_{0}", p.id);
            
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            bool actionPeformed = false;

            long amount = 0;

            Int64.TryParse(this.txtAmount.Text, out amount);

            if (amount != 0)
            {
                ControlCollection repeaterRows = Utilities.FindControlRecursive(this.Master,"RepeaterList").Controls;
                HtmlInputCheckBox cb;

                foreach (Control row in repeaterRows)
                {
                    cb = (System.Web.UI.HtmlControls.HtmlInputCheckBox)row.FindControl("chkAction");
                    if (cb.Checked)
                    {
                        int? ObjectId = Utilities.StringToInt(cb.Value.Replace("userId_", ""));

                        if (ObjectId.HasValue)
                        {
                            bool result = AddFundsForSingleUser(ObjectId.Value, amount);

                            if (result)
                            {
                                InsertEventLogEntry("User [" + ObjectId.Value.ToString() + "] funds updated.");

                                if (!actionPeformed)
                                    actionPeformed = true;
                            }

                        }
                    }

                }

            }

            if (actionPeformed)
            {

                this.Redirect("/Secured/Payments/Default.aspx?Ns=3&payment=ok");
            }
        }


        protected bool AddFundsForSingleUser(int Id, long amount)
        {
            user u = this.BusinessFacade.UserBL.GetSingleUser(Id);

            if (u != null)
            {
                u.funds += amount;

                if (this.BusinessFacade.UserBL.UpdateUser())
                {
                    fundsLog logEntry = new fundsLog();

                    logEntry.fromUserId = this.LoggedInUser.id;
                    logEntry.toUserId = u.id;
                    logEntry.dateCreated = DateTime.Now;

                    logEntry.syndicateId = this.LoggedInUser.syndicateId;

                    if (amount > 0)
                    {
                        logEntry.description = "Funds added to account.";
                        logEntry.moneyIn = true;
                        logEntry.amount = amount;
                    }
                    else
                    {
                        logEntry.description = "Funds removed from account.";
                        logEntry.moneyIn = false;
                        logEntry.amount = -amount;
                    }

                    this.BusinessFacade.FundsLogBL.InsertEntry(logEntry);


                    return true;

                }
                else
                {
                    return false;
                }


            }

            return false;
        }


        protected string PrintPlayerName(Object data)
        {
            user p = data as user;

            return p.username;

        }
    }
}